
Page({
  /**
   * 页面的初始数据
   */
  data: {
    inputText: "",
    btnActive: false,
    info: '',
    showit: false,
    code: '获取',
    name: '-1',
    idcard: '-1',
    phone: '-1',
    coder: "-1",
    can: false
  },

  getcode: function() {
    var vm = this
    if (vm.data.phone == "") {
      wx.showToast({
        title: '请填写手机号',
      })
      return
    }
    var data = {
      "userId": wx.getStorageSync("userId"),
      "mobile": vm.data.phone,
    }
    var userIds = wx.getStorageSync("userId") === "" ? -1 : wx.getStorageSync("userId");
    getApp().requestLoading(getApp().globalData.https + "userResource/verCode/" + vm.data.phone + "/" + wx.getStorageSync("userId"),
      "post", data, "正在请求..",
      function(res) {
        if (res.code == 0) {
          vm.setData({
            code: 9
          })
          var codeinter = setInterval(function() {
            if (vm.data.code <= 1) {
              clearInterval(codeinter)
              vm.setData({
                code: "获取"
              })
            } else {
              vm.setData({
                code: vm.data.code - 1
              })
            }
          }, 1000)
        }
      },
      function(res) {});

  },
  namechange: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  codechange: function(e) {
    this.setData({
      coder: e.detail.value
    })
  },
  idcardchange: function(e) {
    this.setData({
      idcard: e.detail.value
    })
  },

  //首页基本信息
  findHomeInfo: function () {
    var that = this;
    var parm = { "userId": wx.getStorageSync("userId") }
    getApp().requestLoading(getApp().globalData.https + "distributionActivity/distribution/generalizeHome",
      "get", parm, "加载中...",
      function (res) {
        var data = res[0];
        if (data.code == "0") {
          // 获取系统信息   
          var list = data.list[0];
          that.setData({
            info: list
          })
        }
      }
    )
  },
  phonechange: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  commit: function() {
    if (this.data.name == "-1") {
      this.setData({
        name: ''
      })
      return
    }
    if (this.data.phone == "-1") {
      this.setData({
        phone: ''
      })
      return
    }
    var vm = this
    var data = {
      "userId": wx.getStorageSync("userId"),
      "mobile": vm.data.phone,
      "verCode": vm.data.coder,
      "name": vm.data.name,
    }
    var userIds = wx.getStorageSync("userId") === "" ? -1 : wx.getStorageSync("userId");
    getApp().requestLoading(getApp().globalData.https + "userResource/checkVerCode",
      "post", data, "正在请求..",
      function(res) {
        if (res.code == 0) {
          var data = {
            "userId": wx.getStorageSync("userId"),
            "type": "0",
            "price": vm.data.inputText,
          }
          getApp().requestLoading(getApp().globalData.https + "orderResource/withdraw/" + wx.getStorageSync("userId") + "/0/" + vm.data.inputText,
            "post", data, "正在请求..",
            function(res) {
              if (res.code == 0) {
                wx.showToast({
                  title: '申请成功',
                })
                vm.setData({
                  showit: false,
                })
              }
            },
            function(res) {
              wx.showToast({
                title: '申请失败',
              })
              vm.setData({
                showit: false,
              })
            });


          vm.setData({
            showit: false,
            can: true
          })
        }
      },
      function(res) {
        this.setData({
          showit: false,
        })
      });

  },
  close: function() {
    this.setData({
      showit: false,
    })
  },

  withdraw: function() {
    if (!this.data.btnActive) {
      wx.showToast({
        title: '大于可提现金额',
      })
      return
    }
    if (this.data.inputText == "") {
      wx.showToast({
        title: '请输入提现金额',
      })
      return
    }


    if (this.data.inputText <= 0) {
      wx.showToast({
        title: '金额数额错误',
      })
      return
    }
    this.setData({
      showit: true,
    })

    return
    if (this.data.can) {
      var vm = this
      var data = {
        "userId": wx.getStorageSync("userId"),
        "type": '1',
        "price": vm.data.inputText
      }
      var userIds = wx.getStorageSync("userId") === "" ? -1 : wx.getStorageSync("userId");
      getApp().requestLoading(getApp().globalData.https + "/orderResource/withdraw/" + wx.getStorageSync("userId") + "/1/" + vm.data.inputText,
        "post", data, "正在提交..",
        function(res) {
          if (res.code == 0) {
            // vm.setData({
            //   info: res.data,
            // })
          }
        },
        function(res) {});

    } else {
      this.setData({
        showit: true,
      })
    }

  },
  moneyInput: function(e) {
    this.setData({
      inputText: e.detail.value
    });
    console.log(this.data.inputText);
    if (this.data.inputText != "" || this.data.inputText.length != 0) {
      if (this.data.inputText > this.data.info.extractBalance) {
        this.setData({
          btnActive: false
        });
      } else {
        this.setData({
          btnActive: true
        });
      }
    } else {
      this.setData({
        btnActive: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // var vm = this
    // var data = {}
    // var userIds = wx.getStorageSync("userId") === "" ? -1 : wx.getStorageSync("userId");
    // getApp().request(getApp().globalData.https + "/orderResource/walletBalance/" + userIds,
    //   "get", data,
    //   function(res) {
    //     if (res.code == 0) {
    //       vm.setData({
    //         info: res.data,
    //       })
    //     }
    //   },
    //   function(res) {});
    this.findHomeInfo()
  },
})